package com.shelpe.services.sinbad.controller;

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.shelpe.services.sinbad.controller.LogDto.LogNoteable;
import com.shelpe.services.sinbad.entity.jpa.BehaviorTag;
import com.shelpe.services.sinbad.entity.jpa.BuyerBehaviorAnalysis;

import lombok.Data;

@Data
@JsonInclude(Include.NON_DEFAULT)
public class LogDto<T extends LogNoteable> {
	private final static String[] TEMPLATE = new String[0];
	/** 计划，创意，推广组，关键词分页 */
	@JsonProperty("updated_time")
	@JsonFormat(shape = Shape.STRING, pattern = "MM-dd HH:mm")
	private String updatedTime;
	private String action;
	@JsonProperty("new_int_val")
	private long newValue = -1;
	private T note;
	private String keyword;
	/** 消息分页 */
	@JsonProperty("title")
	private String name;
	@JsonProperty("inform_id")
	private long informID;
	@JsonProperty("flag_form")
	private String flagForm;
	@JsonProperty("is_dialog")
	private String isDialog;
	private String content;
	@JsonProperty("create_time")
	@JsonFormat(shape = Shape.STRING, pattern = "yyyy-MM-dd")
	private String createTime;
	@JsonProperty("read")
	private int unread = -1;
	/** 自然搜索分页 */
	private int click = -1;
	@JsonProperty("fav_count")
	private int favCount = -1;
	@JsonProperty("cart_count")
	private int cartCount = -1;
	@JsonProperty("pay_total")
	private int payTotal = -1;
	@JsonProperty("pay_count")
	private int payCount = -1;
	/** 直通车用户分页 */
	private String nick;
	@JsonProperty("behavior_name")
	private String[] behaviorNames;
	@JsonProperty("behavior_display_name")
	private String[] behaviorDisplayNames;
	@JsonProperty("visit_depth")
	private int visitDepth = -1;

	public static interface LogNoteable {

	}

	public void from(BuyerBehaviorAnalysis buyer, List<BehaviorTag> tags) {
		if ((buyer.getBehavior() & 1 << 3) != 0) {
			BehaviorTag tag = new BehaviorTag();
			tag.setTagID(4);
			tag.setTagName("buy");
			tags.add(tag);
		}

		if ((buyer.getBehavior() & 1 << 2) != 0) {
			BehaviorTag tag = new BehaviorTag();
			tag.setTagID(3);
			tag.setTagName("cart");
			tags.add(tag);
		}

		if ((buyer.getBehavior() & 1 << 1) != 0) {
			BehaviorTag tag = new BehaviorTag();
			tag.setTagID(2);
			tag.setTagName("fav");
			tags.add(tag);
		}

		if ((buyer.getBehavior() & 1 << 0) != 0) {
			BehaviorTag tag = new BehaviorTag();
			tag.setTagID(1);
			tag.setTagName("click");
			tags.add(tag);
		}
		this.setKeyword(buyer.getKeywordstr());
	}

	public void from(List<BehaviorTag> tags) {
		if (tags != null) {
			List<String> names = new ArrayList<String>();
			List<String> displays = new ArrayList<String>();
			for (BehaviorTag tag : tags) {
				names.add(tag.getTagName());
				if (tag.getDisplayName() != null) {
					displays.add(tag.getDisplayName());
				}
			}
			this.behaviorNames = names.toArray(TEMPLATE);
			this.behaviorDisplayNames = displays.toArray(TEMPLATE);
		}
	}
}
